/**
 * PaperController.java
 * Version 1.0.0
 * Created on 2018年7月14日
 */
package com.yincheng.oes.business.exam.createpaper.controller;

import com.yincheng.oes.business.exam.base.entity.Paper;
import com.yincheng.oes.business.exam.createpaper.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.UUID;

import static com.yincheng.oes.business.util.PaperUtils.getAnswersList;

/**
 * @author yangsy
 * @version 1.0.0
 * @since 创建时间：2018年7月14日
 * 功能描述：
 * 邮箱地址：446948434@qq.com
 */
@Controller
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    private PaperService paperService;

    /**
     * 创建试卷
     *
     * @param view
     * @return
     */
    @RequestMapping(value = "/draftPaper", method = RequestMethod.GET)
    public ModelAndView draftPaper(ModelAndView view) {
        setView(view);
        String id = UUID.randomUUID().toString();
        view.addObject("paperId", id);
        view.setViewName("redirect:/paper/editPaperPage.html");
        return view;
    }

    /**
     * 编辑试卷页面
     *
     * @param view
     * @param paperId
     * @return
     */
    @RequestMapping(value = "/editPaperPage.html", method = RequestMethod.GET)
    public ModelAndView editPaperPage(ModelAndView view, String paperId) {
        setView(view);
        Paper paper = paperService.getPaper(paperId);
        view.addObject("form", paper);
        view.setViewName("/business/paper/paper-edit");
        return view;
    }

    /**
     * 只读试卷页面
     *
     * @param view
     * @param paperId
     * @return
     */
    @RequestMapping(value = "/readPaperPage.html", method = RequestMethod.GET)
    public ModelAndView readPaperPage(ModelAndView view, String paperId) {
        setView(view);
        Paper paper = paperService.getPaper(paperId);
        view.addObject("form", paper);
        view.setViewName("/business/paper/paper-read");
        return view;
    }

    /**
     * 根据状态跳转到不同状态的试卷页面
     *
     * @param view
     * @param paperId
     * @return
     */
    @RequestMapping(value = "/openAndJumpPaperPage", method = RequestMethod.GET)
    public ModelAndView openAndJumpPaperPage(ModelAndView view, String paperId) {
        view.addObject("paperId", paperId);
        Paper paper = paperService.getPaper(paperId);
        if ("1".equals(paper.getStatus())) {
            view.setViewName("redirect:/paper/editPaperPage.html");
        } else {
            view.setViewName("redirect:/paper/readPaperPage.html");
        }
        return view;
    }

    /**
     * 保存试卷信息
     *
     * @param paper
     */
    @RequestMapping(value = "/savePaper.json", method = RequestMethod.POST)
    public void savePaper(Paper paper) {
        paperService.savePaper(paper);
    }

    /**
     * 发布并保存试卷信息
     *
     * @param paper
     */
    @RequestMapping(value = "/submitPaper.json", method = RequestMethod.POST)
    public void submitPaper(Paper paper) {
        paper.setStatus("2");// 已发布
        paperService.savePaper(paper);
    }

    @InitBinder // 类初始化是调用的方法注解
    public void initBinder(WebDataBinder binder) {
        // 给这个controller配置接收list的长度1024，仅在这个controller有效
        binder.setAutoGrowCollectionLimit(1024);
    }

    private void setView(ModelAndView view) {
        List<String> answers = getAnswersList();
        view.addObject("answers", answers);
    }

    @RequestMapping(value = "/changeStatus.json", method = RequestMethod.POST)
    public void changeStatus(String paperId) {
        paperService.changeStatus(paperId);
    }

    @RequestMapping(value = "/copyPaper.json", method = RequestMethod.POST)
    public void copyPapers(String paperId) {
        paperService.copyPaper(paperId);
    }
}
